package chixing.day20.proxy.homework.work2;

import java.util.*;

public class MemberService {
    private static Map<Integer,Member> memberMap = new HashMap<>();
    /*
     * 根据入住记录为会员累计积分
     * */
    public void recordStay(OrderRecord record) {
        if (record.getRoomType().equals("套房")) {
            double m = record.getMember().getCord() + 30 * record.getSumDay();
            record.getMember().setCord(m);
        }
        if (record.getRoomType().equals("高级房")) {
            double m = record.getMember().getCord() + 20 * record.getSumDay();
            record.getMember().setCord(m);
        }
        if (record.getRoomType().equals("普通房")) {
            double m = record.getMember().getCord() + 10 * record.getSumDay();
            record.getMember().setCord(m);
        }
        memberMap.put(record.getMember().getVipId(),record.getMember());
    }

    /*
    * 获取积分最高的N位会员
    * */
    public List<Member> getTopNMembers(int n){
        List<Member> memberList = new ArrayList<>();//要返回的集合
        List<Member> list = new ArrayList<>();
        //给map里面的用户的积分进行排序
        //先把map集合中的member对象取出来放到list中
        for (Map.Entry<Integer,Member> e: memberMap.entrySet()) {
            list.add( e.getValue());
        }
        //在list中按照member的积分进行排序
        list.sort(Comparator.comparingDouble(Member::getCord).reversed());
        for (int i = 0; i < n; i++) {
            memberList.add(list.get(i));
        }
        return memberList;
    }

    /*
     * 按会员等级分组（青铜<2000，白银<5000，黄金≥5000）
     * */
    public Map<String,List<Member>> groupByLevel(){
        //等级map 青铜，白银，黄金
        Map<String, List<Member>> map = new HashMap<>();
        //青铜等级的会员列表
        List<Member> memberList1 = new ArrayList<>();
        //白银等级的会员列表
        List<Member> memberList2 = new ArrayList<>();
        //黄金等级的会员列表
        List<Member> memberList3 = new ArrayList<>();
        for (Map.Entry<Integer,Member> e: memberMap.entrySet()) {
            if (e.getValue().getCord() >= 5000){
                memberList3.add(e.getValue());
            }else if (e.getValue().getCord()>2000){
                memberList2.add(e.getValue());
            }else {
                memberList1.add(e.getValue());
            }
        }
        map.put("青铜",memberList1);
        map.put("白银",memberList2);
        map.put("黄金",memberList3);
        return  map;
    }

}
